package com.Sake.system.service.impl;

import com.Sake.common.constant.ReturnMsg;
import com.Sake.common.utils.MultipartFileToFileUtil;
import com.Sake.common.utils.result.ResultUtil;
import com.Sake.common.utils.tengxunyun.TenXunUtils;
import com.Sake.system.mapper.HomeImageMapper;
import com.Sake.system.service.HomeImageService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;

/**
 * @author zsh
 * @date 2021.7.1
 */
@Service
public class HomeImageServiceImpl implements HomeImageService {


    @Autowired
    private HomeImageMapper homeImageMapper;

    @Autowired
    private ResultUtil resultUtil;


    /**
     * 获取所有图片列表
     *
     * @return
     */
    @Override
    public JSONObject getAllImages() {
        JSONObject data = homeImageMapper.getAllImages();
        return resultUtil.resultSuccessData(data);
    }

    /**
     * 单个修改图片
     *
     * @param file
     * @param index
     * @return
     */
    @Override
    public JSONObject editImage(MultipartFile file, Integer index) throws Exception {
        if (index == null) {
            return resultUtil.resultCustomMsg(ReturnMsg.FAILED, "修改图片的index不能为空");
        }
        //将MultipartFile转换为File
        File image = MultipartFileToFileUtil.multipartFileToFile(file);
        String newName = "";
        switch (index) {
            case 1:
                //依据index改名文件名达到上传时覆盖原腾讯云存储中的原文件
                newName = "backgroundImage";//主页背景图
                break;
            case 2:
                newName = "headPortrait";//主页头像图
                break;
            case 3:
                newName = "backgroundImage_journal";//主页日志背景图
                break;
            case 4:
                newName = "headPortrait_journal";//主页日志头像图
                break;
            case 5:
                newName = "backgroundImage_photoAlbum";//主页相册背景图
                break;
            case 6:
                newName = "headPortrait_photoAlbum";//主页相册头像图
                break;
            case 7:
                newName = "backgroundImage_personalFile";//主页个人档背景图
                break;
            case 8:
                newName = "headPortrait_personalFile";//主页个人档头像图
                break;
            case 9:
                newName = "backgroundImage_share";//主页分享背景图
                break;
            case 10:
                newName = "headPortrait_share";//主页分享头像图
                break;
            case 11:
                newName = "loginPageBackground";//登录页背景图
                break;
            case 12:
                newName = "loginPageAvatar";//登录页头像图
                break;
            default:
                return resultUtil.resultCustomMsg(ReturnMsg.FAILED, "修改图片的index只能为1,2,3,4其中一个");
        }
        File newFile = MultipartFileToFileUtil.rename(image, newName);
        //上传到腾讯云
        String uploadUrl = TenXunUtils.upload(newFile);
        MultipartFileToFileUtil.close(newFile);
        //修改数据库图片相应地址
        homeImageMapper.editImage(uploadUrl, index);
        return resultUtil.resultSuccess();
    }

    /**
     * 多图修改
     *
     * @param backgroundImage              主页背景
     * @param headPortrait                 主页头像
     * @param backgroundImage_journal      主页日志背景
     * @param headPortrait_journal         主页日志头像
     * @param backgroundImage_photoAlbum   主页相册背景
     * @param headPortrait_photoAlbum      主页相册头像
     * @param backgroundImage_personalFile 主页个人档背景
     * @param headPortrait_personalFile    主页个人档头像
     * @param backgroundImage_share        主页分享背景
     * @param headPortrait_share           主页分享头像
     * @param loginPageBackground          登录页背景
     * @param loginPageAvatar              登录页头像
     * @return response
     * @throws Exception 将MultipartFile转换为File异常
     */
    @Override
    public JSONObject editImages(MultipartFile backgroundImage, MultipartFile headPortrait, MultipartFile backgroundImage_journal, MultipartFile headPortrait_journal, MultipartFile backgroundImage_photoAlbum, MultipartFile headPortrait_photoAlbum, MultipartFile backgroundImage_personalFile, MultipartFile headPortrait_personalFile, MultipartFile backgroundImage_share, MultipartFile headPortrait_share, MultipartFile loginPageBackground, MultipartFile loginPageAvatar) throws Exception {

        HashMap<String, File> hashMap = new HashMap<>();
        if (backgroundImage != null && backgroundImage.getSize() > 0) {
            //将MultipartFile转换为File
            File image = MultipartFileToFileUtil.multipartFileToFile(backgroundImage);
            File newFile = MultipartFileToFileUtil.rename(image, "backgroundImage");
            hashMap.put("backgroundImage", newFile);
            MultipartFileToFileUtil.close(image);
        }
        if (headPortrait != null && headPortrait.getSize() > 0) {
            File image = MultipartFileToFileUtil.multipartFileToFile(headPortrait);
            File newFile = MultipartFileToFileUtil.rename(image, "headPortrait");
            hashMap.put("headPortrait", newFile);
            MultipartFileToFileUtil.close(image);
        }
        if (backgroundImage_journal != null && backgroundImage_journal.getSize() > 0) {
            File image = MultipartFileToFileUtil.multipartFileToFile(backgroundImage_journal);
            File newFile = MultipartFileToFileUtil.rename(image, "backgroundImage_journal");
            hashMap.put("backgroundImage_journal", newFile);
            MultipartFileToFileUtil.close(image);
        }
        if (headPortrait_journal != null && headPortrait_journal.getSize() > 0) {
            File image = MultipartFileToFileUtil.multipartFileToFile(headPortrait_journal);
            File newFile = MultipartFileToFileUtil.rename(image, "headPortrait_journal");
            System.out.println(newFile.isFile());
            hashMap.put("headPortrait_journal", newFile);
            MultipartFileToFileUtil.close(image);
        }
        if (backgroundImage_photoAlbum != null && backgroundImage_photoAlbum.getSize() > 0) {
            File image = MultipartFileToFileUtil.multipartFileToFile(backgroundImage_photoAlbum);
            File newFile = MultipartFileToFileUtil.rename(image, "backgroundImage_photoAlbum");
            hashMap.put("backgroundImage_photoAlbum", newFile);
            MultipartFileToFileUtil.close(image);
        }
        if (headPortrait_photoAlbum != null && headPortrait_photoAlbum.getSize() > 0) {
            File image = MultipartFileToFileUtil.multipartFileToFile(headPortrait_photoAlbum);
            File newFile = MultipartFileToFileUtil.rename(image, "headPortrait_photoAlbum");
            hashMap.put("headPortrait_photoAlbum", newFile);
            MultipartFileToFileUtil.close(image);
        }
        if (backgroundImage_personalFile != null && backgroundImage_personalFile.getSize() > 0) {
            File image = MultipartFileToFileUtil.multipartFileToFile(backgroundImage_personalFile);
            File newFile = MultipartFileToFileUtil.rename(image, "backgroundImage_personalFile");
            hashMap.put("backgroundImage_personalFile", newFile);
            MultipartFileToFileUtil.close(image);
        }
        if (headPortrait_personalFile != null && headPortrait_personalFile.getSize() > 0) {
            File image = MultipartFileToFileUtil.multipartFileToFile(headPortrait_personalFile);
            File newFile = MultipartFileToFileUtil.rename(image, "headPortrait_personalFile");
            hashMap.put("headPortrait_personalFile", newFile);
            MultipartFileToFileUtil.close(image);
        }
        if (backgroundImage_share != null && backgroundImage_share.getSize() > 0) {
            File image = MultipartFileToFileUtil.multipartFileToFile(backgroundImage_share);
            File newFile = MultipartFileToFileUtil.rename(image, "backgroundImage_share");
            hashMap.put("backgroundImage_share", newFile);
            MultipartFileToFileUtil.close(image);
        }
        if (headPortrait_share != null && headPortrait_share.getSize() > 0) {
            File image = MultipartFileToFileUtil.multipartFileToFile(headPortrait_share);
            File newFile = MultipartFileToFileUtil.rename(image, "headPortrait_share");
            hashMap.put("headPortrait_share", newFile);
            MultipartFileToFileUtil.close(image);
        }
        if (loginPageBackground != null && loginPageBackground.getSize() > 0) {
            File image = MultipartFileToFileUtil.multipartFileToFile(loginPageBackground);
            File newFile = MultipartFileToFileUtil.rename(image, "loginPageBackground");
            hashMap.put("loginPageBackground", newFile);
            MultipartFileToFileUtil.close(image);
        }
        if (loginPageAvatar != null && loginPageAvatar.getSize() > 0) {
            File image = MultipartFileToFileUtil.multipartFileToFile(loginPageAvatar);
            File newFile = MultipartFileToFileUtil.rename(image, "loginPageAvatar");
            hashMap.put("loginPageAvatar", newFile);
            MultipartFileToFileUtil.close(image);
        }
        JSONObject uploadUrls = new JSONObject();
        //通过hashmap的key确定字段 value存储file图片 循环上传腾讯云并且记录url返回给uploadUrls
        hashMap.keySet().stream().forEach(str -> {
            //上传到腾讯云
            String uploadUrl = TenXunUtils.upload(hashMap.get(str));
            MultipartFileToFileUtil.close(hashMap.get(str));
            uploadUrls.put(str, uploadUrl);
        });
        //修改数据库图片相应地址
        homeImageMapper.editImages(uploadUrls);
        return resultUtil.resultSuccess();
    }

    /**
     * 动态图片单个上传
     *
     * @param contentImage 图片
     * @return 上传成功后返回图片地址
     */
    @Override
    public JSONObject updetImage(MultipartFile contentImage) throws Exception {
        if (contentImage != null && contentImage.getSize() > 0) {
            //将MultipartFile转换为File
            File image = MultipartFileToFileUtil.multipartFileToFile(contentImage);
            //上传地址
            String upload = TenXunUtils.upload(image);
            MultipartFileToFileUtil.close(image);
            //返回地址
            return resultUtil.resultSuccessData(upload);
        }
        return resultUtil.resultCustomMsg(ReturnMsg.PARAMETER_EXC,"文件不能为空");
    }

    /**
     * 根据图片地址删除腾讯云上的图片
     *
     * @param imageUrl 图片地址
     * @return
     */
    @Override
    public JSONObject delectUmage(String imageUrl) {

        return null;
    }


}
